package com.yihusitian.zsxq.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yihusitian.zsxq.pojo.data.UserDO;
import com.yihusitian.zsxq.pojo.dto.Question;
import com.yihusitian.zsxq.service.IDGenerator;
import com.yihusitian.zsxq.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yihusitian.zsxq.service.QuestionService;
import com.yihusitian.zsxq.pojo.data.QuestionDO;
import com.yihusitian.zsxq.mapper.QuestionMapper;
import org.springframework.util.IdGenerator;

/**
 * @Description TODO
 * @Author LeeHo
 * @Date 2021-03-25 11:49:27
 */
@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, QuestionDO> implements QuestionService {

    @Autowired
    private UserService userService;

    @Override
    public QuestionDO createQuestion(Question question, Long topicId) {
        if (question == null) {
            return null;
        }
        UserDO owner = userService.createOrUpdate(question.getOwner());
        UserDO questionee = userService.createOrUpdate(question.getQuestionee());
        QuestionDO questionDO = QuestionDO.builder()
                .id(IDGenerator.generateId())
                .ownerId(owner == null ? 0L : owner.getId())
                .questioneeUserId(questionee == null ? 0L : questionee.getId())
                .topicId(topicId)
                .text(StrUtil.emptyToDefault(question.getText(), StrUtil.EMPTY))
                .build();
        questionDO.insert();
        return questionDO;
    }
}